<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${namespace}">
	<!-- 结果集Map -->
	<resultMap id="BaseResultMap" type="${modelClass}">
		<#list table.columns as column>
		<result column="${column.name}" property="${column.name}" jdbcType="${column.dataType.jdbcTypeName?upper_case}"/>
		</#list>
	</resultMap>
	 
	
	<!-- 数据表中的所有列 -->
	<sql id="Base_Column_List">
	${space}<@compress single_line=true>
		<#list table.columns as column>
			${column.name}<#if (column_index lt table.columns?size-1)>,</#if>
		</#list>
	</@compress>
	
	</sql>
	
	<!-- 保存 -->
	<insert id="save" parameterType="${modelClass}" useGeneratedKeys="true" keyProperty="id">
		insert into ${table.name}(
		${tab}<@compress single_line=true>
		<#list table.baseColumns as column>
			${column.name}<#if (column_index lt table.baseColumns?size-1)>,</#if>
		</#list>
		</@compress>)
		values(
			<#list table.baseColumns as column>
	<#if (column_index == 0)>${tab}${tab}${tab}</#if>${column.placeholderSimpleName}<#if (column_index lt table.baseColumns?size-1)>,</#if>
			</#list>)
	</insert>
	
	<!-- 保存不为空的属性 -->
	<insert id="saveSelective" parameterType="${modelClass}" useGeneratedKeys="true" keyProperty="id">
		insert into ${table.name}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list table.baseColumns as column>
			<if test="${column.name} != null">${column.name},</if>
			</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<#list table.baseColumns as column>
			<if test="${column.name} != null">${column.placeholderSimpleName},</if>
			</#list>
		</trim>
	</insert>
	
	<!-- 修改 -->
	<update id="update" parameterType="${modelClass}">
		update ${table.name} set
			<#list table.baseColumns as column>
			${column.name} = ${column.placeholderSimpleName}<#if (column_index lt table.baseColumns?size-1)>,</#if>
			</#list>
		where id = ${table.pkPlaceholderSimpleName}
	</update>
	
	<!-- 修改不为空的属性 -->
	<update id="updateSelective" parameterType="${modelClass}">
		update ${table.name}
		<set>
			<#list table.baseColumns as column>
			<if test="${column.name} != null">${column.name} = ${column.placeholderSimpleName},</if>
			</#list>
		</set>
		where id = ${table.pkPlaceholderSimpleName}
	</update>
	
	<!-- 根据ID删除 -->
	<delete id="delete" parameterType="java.lang.Integer">
		delete from ${table.name} where id = ${table.pkPlaceholderSimpleName}
	</delete>
	
	<!-- 根据ID获取 -->
	<select id="get" parameterType="java.lang.Integer" resultType="${modelClass}">
		select
			<include refid="Base_Column_List"/> 
		from ${table.name}
		where id = ${table.pkPlaceholderSimpleName}
	</select>
	
	<!-- 根据条件获取ids -->
	<select id="getIds" resultType="java.lang.Integer">
		select id from ${table.name}
		<where>
			<#list table.baseColumns as column>
			<if test="${column.name}!=null">and ${column.name} = ${column.placeholderSimpleName}</if>
			</#list>
		</where>
	</select>
	
	<!-- 根据条件获取个数 -->
	<select id="getCount" resultType="java.lang.Integer">
		select count(1) from ${table.name}
		<where>
			<#list table.baseColumns as column>
			<if test="${column.name}!=null">and ${column.name} = ${column.placeholderSimpleName}</if>
			</#list>
		</where>
	</select>
	
	<!-- 根据条件获取列表 -->
	<select id="getList" parameterType="java.util.Map" resultType="${modelClass}">
		select
			<include refid="Base_Column_List"/> 
		from ${table.name}
		<where>
			<#list table.baseColumns as column>
			<if test="${column.name}!=null">and ${column.name} = ${column.placeholderSimpleName}</if>
			</#list>
		</where>
		order by id
	</select>
	
	<!-- 根据条件获取分页列表 -->
	<select id="getPageList" parameterType="java.util.Map" resultType="java.util.Map">
		select
			<include refid="Base_Column_List"/> 
		from ${table.name} 
		<where>
			<#list table.baseColumns as column>
			<if test="${column.name}!=null">and ${column.name} = ${column.placeholderSimpleName}</if>
			</#list>
			<if test="keyword!=null">
				<bind name="keywordbind" value="'%'+_parameter.keyword+'%'"/>
					and (
						<#list table.baseColumns as column>
						${column.name} like ${keywordbind}<#if (column_index lt table.baseColumns?size-1)> or</#if>
						</#list>
					)
			</if>
		</where>
	</select>
	
	<!-- 批量保存 -->
	<insert id="batchSave" parameterType="java.util.Map">
		insert into ${table.name} (
			<#list table.baseColumns as column>
				${column.name}<#if (column_index lt table.baseColumns?size-1)>,</#if>
			</#list>
			)
		values
		<foreach item="item" collection="collection" index="index" separator=",">
			(
				<#list table.baseColumns as column>
				${prefix}item.${column.name}${suffix}<#if (column_index lt table.baseColumns?size-1)>,</#if>
				</#list>
			)
		</foreach>
	</insert>
	
	<!-- 批量修改 -->
	<update id="batchUpdate" parameterType="java.util.Map">
		update ${table.name} 
		<set>
			<#list table.baseColumns as column>
			<if test="object.${column.name} != null">${column.name} = ${prefix}object.${column.name}${suffix},</if>
			</#list>
		</set>	
		where id in
		<foreach item="item" collection="ids" index="index" separator="," open="(" close=")">
			${prefix}item${suffix}
		</foreach>
	</update>
	
	<!-- 批量删除 -->
	<delete id="batchDelete" parameterType="java.util.Map">
		delete from ${table.name} where id in
		<foreach item="item" collection="ids" index="index" separator="," open="(" close=")">
			${prefix}item${suffix}
		</foreach>
	</delete>
	
</mapper>